package service;

import java.util.List;
import java.util.Map;

import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionContext;

import dao.EmploymentDao;
import dao.IEmploymentDao;
import dao.InviteDao;
import dao.SearchDao;
import po.EmpHistory;
import po.Employment;
import po.Order;
import po.User;

public class EmploymentService implements IEmploymentService{
	private IEmploymentDao employmentDao;

	public void setEmploymentDao(IEmploymentDao employmentDao) {
		this.employmentDao = employmentDao;
	}
	
	private List<Employment> list;
	private Map<String, Object> request, session;
	
	//显示自己名下的招聘信息
	public List getAllEmployment(int id) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		String hql = "from Employment as emp where costomerID="+id;
		list = employmentDao.findByHql(hql);	
		//由返回数组得到部分统计信息---不完整
//		int number = list.size(); //当前课程总数
//		int math = 0;
//		int english = 0;
//		int china = 0;
//		for(int i=0;i<list.size();i++) {
//			if(list.get(i).getCourse().equals("数学")) {
//				math++;
//			}
//			if(list.get(i).getCourse().equals("语文")) {
//				china++;
//			}
//			if(list.get(i).getCourse().equals("英语")) {
//				english++;
//			}
//		}
		//request.put("info", "共收录："+number+"条招聘信息");
		return list;
	}
	//申请信息-查看家教信息
	public User searchApplyInfo(int empID) {
		Employment emp = (Employment)employmentDao.findEmpInfo(empID);
		
		int tutorID = emp.getPromulgatorID();
		User user = (User)employmentDao.findUserInfo(tutorID);
		return user;
	}
	//接受申请
	public boolean accept(int empID) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		Employment emp = (Employment)employmentDao.findEmpInfo(empID);
		emp.setStatus(2);
		
		employmentDao.updateStatus(emp);
			
		request.put("info", "成功接受申请！");
		return true;
	}
	//拒绝申请
	public boolean refuse(int empID) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		Employment emp = (Employment)employmentDao.findEmpInfo(empID);
		emp.setStatus(0);
		emp.setPromulgatorID(null);
		
		employmentDao.updateStatus(emp);
			
		request.put("info", "成功拒绝申请！");
		return true;
	}
	//确认完成
	public boolean finish(int empID) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		Employment emp = (Employment)employmentDao.findEmpInfo(empID);
		
		EmpHistory history = new EmpHistory(emp);
		employmentDao.deleteEmp(emp);
		employmentDao.saveHistory(history);
		request.put("info", "招聘课程成功结束！");
		return true;
	}
	//删除招聘信息
	public boolean delete(int empID) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		Employment emp = (Employment)employmentDao.findEmpInfo(empID);
		employmentDao.deleteEmp(emp);
		request.put("info", "已删除招聘请求！");
		return true;
	}
	//修改招聘信息
	public boolean mod(int empID,String money,String date,String address,String text) {
		//System.out.println("date"+date+"date");
		Employment emp = (Employment)employmentDao.findEmpInfo(empID);
		if(money.trim().length()>0) {
			emp.setPrice(Double.parseDouble(money));
		}
		if(date.trim().length()>0) {
			//System.out.println("date");
			emp.setDate(date);
		}
		if(address.trim().length()>0) {
			emp.setAddress(address);
		}
		if(text.trim().length()>0) {
			emp.setNeedOrPresentation(text);
		}
		employmentDao.updateStatus(emp);
		
		return true;
	}
}
